package j.framework.tags.easyui;

import j.util.DES;
import j.util.StringUtils;

import javax.servlet.jsp.JspException;
import java.util.Map;

/**
 * 所有需要从远程加载数据的均需要继承此类（指的是form控件，datagrid不含在内）
 * User: jason
 * Date: 2015/1/16 16:27
 */
public abstract class BaseEasyuiLoadRemoteDataFormControl extends BaseEasyuiComboFormControl {

    protected String url;
    protected String doName;
    protected String actionName;
    protected String params;
    protected String requestId;

    @Override
    public void writeEasyuiDataOptions(Map<String, Object> dataOptions)  throws JspException{
        super.writeEasyuiDataOptions(dataOptions);
        String url = this.url;
        if(StringUtils.isNullOrEmpty(url)){
            /*if(StringUtils.isNullOrEmpty(doName)
                || StringUtils.isNullOrEmpty(actionName)){
                throw new JspException("从远程加载数据的easyui控件必须指定url或doName及actionName参数！");
            }*/
            if(StringUtils.isNotEmpty(doName)
                    && StringUtils.isNotEmpty(actionName)){
                url = String.format("%s/%s?act=%s", "/user",
                        doName,actionName);
            }
            if(StringUtils.isNotEmpty(requestId)){
                url = "/user/req?rid="+ DES.encrypt(requestId);
            }
        }
        if(StringUtils.isNotEmpty(params)){
            url += "&"+params;
        }
        if(StringUtils.isNotEmpty(url)){
            dataOptions.put("url",url);
        }
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public void setDoName(String doName) {
        this.doName = doName;
    }

    public void setActionName(String actionName) {
        this.actionName = actionName;
    }

    public void setParams(String params) {
        this.params = params;
    }

    public void setRequestId(String requestId) {
        this.requestId = requestId;
    }
}
